<?php
class Solution {

    /**
     * @param String[] $strs
     * @return String[][]
     */
    function groupAnagrams($strs) {
        if (empty($strs))
            return [];
        $maps = [];
        foreach ($strs as $str) {
            $map_key = $this->_convert_str_sorted($str);
            
            if (array_key_exists($map_key,  $maps)){
                array_push($maps[$map_key], $str);
            } else {
                $maps[$map_key] = [$str];
            }
        }

        return array_values($maps);
    }

    function _convert_str_sorted($str){
        $rtn = '';
        $map_key = [];
        for ($i=0; $i<strlen($str); $i++){
            $map_key[] = $str[$i];
        }
        sort($map_key);
        foreach ($map_key as $val){
            $rtn .= $val;
        }
        return $rtn;
    }
}
